package cn.yuan.xiaoyu.newsmodule.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.activity.BaseActivity;
import cn.yuan.xiaoyu.commonmodule.adapter.PagerAdapter;
import cn.yuan.xiaoyu.commonmodule.utils.StatusBarCompatUtils;
import cn.yuan.xiaoyu.newsmodule.fragment.NewsFocusFrament;
import cn.yuan.xiaoyu.newsmodule.fragment.NewsHomeFragment;
import cn.yuan.xiaoyu.newsmodule.fragment.NewsMeFrament;
import cn.yuan.xiaoyu.newsmodule.fragment.NewsVideosFragment;

/**
 * Created by yukuoyuan on 2017/3/7.
 * 这是一个新闻的界面
 */

public class NewsActivity extends BaseActivity {
    @Bind(R.id.vp_news_main)
    ViewPager vpNewsMain;
    @Bind(R.id.bt_news_main_tab_home)
    Button btNewsMainTabHome;
    @Bind(R.id.bt_news_main_tab_video)
    Button btNewsMainTabVideo;
    @Bind(R.id.bt_news_main_tab_focus)
    Button btNewsMainTabFocus;
    @Bind(R.id.bt_news_main_tab_me)
    Button btNewsMainTabMe;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void bindPresenter() {

    }

    @Override
    protected void initData(Bundle extras) {
        fragments.add(new NewsHomeFragment());
        fragments.add(new NewsVideosFragment());
        fragments.add(new NewsFocusFrament());
        fragments.add(new NewsMeFrament());
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);
        vpNewsMain.setAdapter(pagerAdapter);
        vpNewsMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                clearstatus();
                switch (position) {
                    case 0:
                        btNewsMainTabHome.setSelected(true);
                        break;
                    case 1:
                        btNewsMainTabVideo.setSelected(true);
                        break;
                    case 2:
                        btNewsMainTabFocus.setSelected(true);
                        break;
                    case 3:
                        btNewsMainTabMe.setSelected(true);
                        break;
                }
            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vpNewsMain.setOffscreenPageLimit(3);
    }

    private void clearstatus() {
        btNewsMainTabHome.setSelected(false);
        btNewsMainTabVideo.setSelected(false);
        btNewsMainTabFocus.setSelected(false);
        btNewsMainTabMe.setSelected(false);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_news;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        clearstatus();
        btNewsMainTabHome.setSelected(true);
    }

    @OnClick({R.id.bt_news_main_tab_home, R.id.bt_news_main_tab_video, R.id.bt_news_main_tab_focus, R.id.bt_news_main_tab_me})
    public void onClick(View view) {
        clearstatus();
        switch (view.getId()) {
            case R.id.bt_news_main_tab_home:
                btNewsMainTabHome.setSelected(true);
                vpNewsMain.setCurrentItem(0, false);
                break;
            case R.id.bt_news_main_tab_video:
                btNewsMainTabVideo.setSelected(true);
                vpNewsMain.setCurrentItem(1, false);
                break;
            case R.id.bt_news_main_tab_focus:
                btNewsMainTabFocus.setSelected(true);
                vpNewsMain.setCurrentItem(2, false);
                break;
            case R.id.bt_news_main_tab_me:
                btNewsMainTabMe.setSelected(true);
                vpNewsMain.setCurrentItem(3, false);
                break;
        }
    }
}
